package zuoye_3_24;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class Zuoye_4 {
    public static void main(String[] args) throws IOException {
        ArrayList<Student> list = new ArrayList<>();
        File file = new File("names2.txt");
        BufferedReader r = new BufferedReader(new FileReader("names1.txt"));
        String line;
        while((line=r.readLine())!=null){
            String arr[] = line.split("-");
            Student student = new Student(arr[0],arr[1],Integer.parseInt(arr[2]),Double.parseDouble(arr[3]));
            list.add(student);
        }
        if(!file.exists()) {
            checkPeople(list);//如果新文件不存在,那么就创建一个20人是文件供点名使用
        }
        //拿取新的数据
        ArrayList<Student> list1 = new ArrayList<>();
        BufferedReader r1 = new BufferedReader(new FileReader("names2.txt"));
        while((line=r1.readLine())!=null){
            String arr[] = line.split("-");
            Student student = new Student(arr[0],arr[1],Integer.parseInt(arr[2]),Double.parseDouble(arr[3]));
            list1.add(student);
        }
        //实现点命
        rollCall(list1);
    }

    private static void rollCall(ArrayList<Student> list) throws IOException {
        //使用一个新的list来存储weight值为1.0是student,再从这个新的list中进行随机点名
        ArrayList<Student> list1 = new ArrayList<Student>();
        Random random = new Random();
        for (Student student : list) {
            if(student.getWeight()==1) {
                list1.add(student);
            }
        }
        //如果新的list已经点完了,list1为空,那么就将所有的weight改为1.0
        if(list1.isEmpty()) {
            for (Student student : list) {
                if(student.getWeight()==0) {
                    list1.add(student);
                }
            }
            list.forEach(student->{
                student.setWeight(1.0);
            });
        }
        //输出点到的人的信息
        int num = random.nextInt(list1.size());
        Student stu1 = list1.get(num);
        System.out.println(stu1.toString1());

        // 修改此学生的信息
        list.forEach(student->{
            if(student.getName().equals(stu1.getName())) {
                student.setWeight(0);
            }
        });
        //将新的数据入文件中
        BufferedWriter w = new BufferedWriter(new FileWriter("names2.txt"));
        for (Student student : list) {
            w.write(student.toString());
            w.newLine();
        }
        w.close();
    }

    private static  void checkPeople(ArrayList<Student> list) throws IOException {
        // TODO Auto-generated method stub
        ArrayList<Student> list1 = new ArrayList<>();
        BufferedWriter w = new BufferedWriter(new FileWriter("names2.txt"));
        for(int i=0;i<10;i++) {
            list1.add(list.get(i));
        }
        for(int i=50;i<60;i++) {
            list1.add(list.get(i));
        }
        for (Student student : list1) {
            w.write(student.toString());
            w.newLine();
        }
        w.close();
    }
}
